import api from '../../api';
import { message } from 'antd';
import { isZiGuanEvir } from '../../utils/commonFunc';

export default {
  namespace: 'bondTradeM', //债券指令
  state: {
    columns: [
      {
        title: '证券代码',
        dataIndex: 'stockCode ',
        key: 'stockCode',
      },
      {
        title: '证券名称',
        dataIndex: 'stockName ',
        key: 'stockName',
      },
      {
        title: '交易方向',
        dataIndex: 'entrustBs ',
        key: 'entrustBs',
        render: (text) => {
          if (text == '1') {
            return '买入';
          } else if (text == '2') {
            return '卖入';
          } else if (text == '4') {
            return '债回收';
          } else {
            return '--';
          }
        },
      },
      {
        title: '价格',
        dataIndex: 'entrustPrice ',
        key: 'entrustPrice',
        type: 'money',
        toFixed: 4,
      },
      {
        title: '数量',
        dataIndex: 'entrustAmount ',
        key: 'entrustAmount',
        type: 'number',
      },
      {
        title: '约定号',
        dataIndex: 'cbpconferId ',
        key: 'cbpconferId',
      },
      {
        title: '发起方交易员',
        dataIndex: 'oppoTraderId ',
        key: 'oppoTraderId',
      },
      {
        title: '发起方主体',
        dataIndex: 'oppoAgency ',
        key: 'oppoAgency',
      },
      {
        title: '执行编号',
        dataIndex: 'execId ',
        key: 'execId',
      },
    ],
  },
  effects: {
    //虚拟标的下达试算
    *virtualTargetTradeBookSave({ payload, callback }, { call, put }) {
      try {
        const response = yield call(api.virtualAddTradeBook, payload);
        callback(response);
      } catch (e) {
        console.log(e);
      }
    },
    //试算
    *trialData({ payload, callback }, { call, put }) {
      const response = yield call(api.trialData, payload);
      if (response && response?.code == 0) {
        callback(response.data);
      } else {
        callback();
        message.error(response?.msg);
      }
    },
    //指令下达新增
    *orderSave({ payload, callback }, { call, put }) {
      const response = yield call(api.addOrder, payload);
      callback(response);
    },
    //簿记新增 修改
    *existenceSave({ payload, callback }, { call, put }) {
      const response = yield call(api.addexistence, payload);
      callback(response);
    },
    //存续管理 修改
    *tradeBookSave({ payload, callback }, { call, put }) {
      const response = yield call(api.addTradeBook, payload);
      callback(response);
    },
    // 获取收益率
    *getRate({ payload, callback }, { call, put }) {
      const response = yield call(api.bondTrade.getRate, payload);
      if (response && response.code == 0) {
        callback(response);
      } else {
        message.error(response ? response.msg : '查询收益率失败!');
      }
    },
    // 获取应计利息
    *getInterest({ payload, callback }, { call, put }) {
      const response = yield call(api.bondTrade.getInterestNew, payload);
      if (response && response.code == 0) {
        callback(response);
      } else {
        message.error(response.msg);
      }
    },

    // 获取下个交易日
    *queryNextTranDt({ payload, callback }, { call, put }) {
      const response = yield call(api.bondTrade.queryNextTranDt, payload);
      if (response && response.success) {
        callback(response.data);
      } else {
        message.error('查询下个交易日失败!');
      }
    },
    // 获取sppi结果
    *getSPPIResult({ payload, callback }, { call, put }) {
      let temp = isZiGuanEvir(); // 判断该页面是在资管被引入的，还是maat,资管环境不改接口不被调用
      if (!!temp) {
        callback();
        return;
      }
      const res = yield call(api.sppiTestRecord.getResult, payload);
      if (res && res.code == '0') {
        callback(res.data || {});
      } else {
        callback();
        message.error('获取SPPI结果失败!');
      }
    },

    // 获取执行编号
    *getExecuteNumber({ payload, callback }, { call, put, select }) {
      const res = yield call(api.bondTrade.getExecuteNumber, payload);

      if (res && res.success) {
        res.columns = yield select((states) => states.bondTradeM.columns);

        callback(res || {});
      } else {
        callback();
        message.error(res?.msg || '获取执行编号失败!');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
